﻿/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using DataModelException = System.Qizx.Api.DataModelException;
using EvaluationException = System.Qizx.Api.EvaluationException;
using Node = System.Qizx.Api.Node;
using QName = System.Qizx.Api.QName;
using QizxException = System.Qizx.Api.QizxException;
using XMLPullStream = System.Qizx.Api.XMLPullStream;
using PushStreamToDOM = System.Qizx.Api.Util.PushStreamToDOM;
using XQDateTimeBase = System.Qizx.Api.Util.XQDateTimeBase;
using DateTimeException = System.Qizx.Api.Util.XQDateTimeException;
using Duration = System.Qizx.Api.Util.XQDuration;
using NamespaceContext = System.Qizx.Util.NamespaceContext;
using Comparison = System.Qizx.Util.Basic.Comparison;
using FileUtil = System.Qizx.Util.Basic.FileUtil;
using ComparisonContext = System.Qizx.XQuery.ComparisonContext;
using XQItem = System.Qizx.XQuery.XQItem;
using XQValue = System.Qizx.XQuery.XQValue;
using StringValue = System.Qizx.XQuery.Dt.StringValue;
using UntypedAtomicType = System.Qizx.XQuery.Dt.UntypedAtomicType;
using URI = System.Uri;

namespace System.Qizx.Xdm
{
    internal class DescendantsOrSelf : TypedSequence
    {
        internal Node lastNode;

        internal DescendantsOrSelf(XdmNode first, NodeFilter nodeTest)
            : base(first, nodeTest)
        {
            try
            {
                lastNode = first.FollowingNode;
            }
            catch (DataModelException snh)
            {
                ; // should not happen
            }
        }

        public override bool Next()
        {
            try
            {
                for (; current != null; )
                {
                    if (started)
                        current = (XdmNode)current.NodeNext();
                    started = true;
                    if (current == lastNode)
                        current = null;
                    else if (checkNode())
                        return true;
                }
            }
            catch (DataModelException shouldNotHappen)
            {
                ;
            }
            return false;
        }

        public override XQValue BornAgain()
        {
            return new DescendantsOrSelf(first, nodeTest);
        }
    }
}
